package com.yanqu.road.dao.impl.activity.snowgarden;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.snowgarden.UserSnowGardenVisitRelation;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserSnowGardenVisitRelationDaoImpl extends TempDataObjectDao<UserSnowGardenVisitRelation> {

    public static String getUserSnowGardenVisitRelationSql = "select * from `t_u_activity_snow_garden_relation` where `activityId`=? and `userId`=? and `targetUserId`=?";

    public UserSnowGardenVisitRelation getUserSnowGardenVisitRelation(int activityId, long userId, long targetUserId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserSnowGardenVisitRelationSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            call.setLong(i++, targetUserId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String getUserSnowGardenVisitRelationListSql = "select * from `t_u_activity_snow_garden_relation` where `activityId`=? and `userId`=?";

    public Map<Long, UserSnowGardenVisitRelation> getUserSnowGardenVisitRelationMap(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserSnowGardenVisitRelation> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserSnowGardenVisitRelationListSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserSnowGardenVisitRelation data = parse(rs);
                dataMap.put(data.getTargetUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String updateUserSnowGardenVisitRelationSql = "update `t_u_activity_snow_garden_relation` set helpMeCount=?,helpHerCount=?,helpMeCount2=?,helpHerCount2=?,helpMeTime=?,helpHerTime=? where activityId=? and userId=? and targetUserId=?";
    @Override
    protected boolean updateData(UserSnowGardenVisitRelation userSnowGardenVisitRelation) {
        userSnowGardenVisitRelation.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getHelpMeCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getHelpHerCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getHelpMeCount2()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getHelpHerCount2()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRelation.getHelpMeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRelation.getHelpHerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRelation.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRelation.getTargetUserId()));
        if (getDb().executeNoneQuery(updateUserSnowGardenVisitRelationSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserSnowGardenVisitRelationSql = "insert into `t_u_activity_snow_garden_relation`(activityId,userId,targetUserId,helpMeCount,helpHerCount,helpMeCount2,helpHerCount2,helpMeTime,helpHerTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserSnowGardenVisitRelation userSnowGardenVisitRelation) {
        userSnowGardenVisitRelation.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRelation.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRelation.getTargetUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getHelpMeCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getHelpHerCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getHelpMeCount2()));
        para.put(i++, new DbParameter(Types.INTEGER, userSnowGardenVisitRelation.getHelpHerCount2()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRelation.getHelpMeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userSnowGardenVisitRelation.getHelpHerTime()));
        if (getDb().executeNoneQuery(insertUserSnowGardenVisitRelationSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserSnowGardenVisitRelation parseData(ResultSet rs) throws SQLException {
        UserSnowGardenVisitRelation userSnowGardenVisitRelation = new UserSnowGardenVisitRelation();
        userSnowGardenVisitRelation.setActivityId(rs.getInt("activityId"));
        userSnowGardenVisitRelation.setUserId(rs.getLong("userId"));
        userSnowGardenVisitRelation.setTargetUserId(rs.getLong("targetUserId"));
        userSnowGardenVisitRelation.setHelpMeCount(rs.getInt("helpMeCount"));
        userSnowGardenVisitRelation.setHelpHerCount(rs.getInt("helpHerCount"));
        userSnowGardenVisitRelation.setHelpMeCount2(rs.getInt("helpMeCount2"));
        userSnowGardenVisitRelation.setHelpHerCount2(rs.getInt("helpHerCount2"));
        userSnowGardenVisitRelation.setHelpMeTime(rs.getLong("helpMeTime"));
        userSnowGardenVisitRelation.setHelpHerTime(rs.getLong("helpHerTime"));

        return userSnowGardenVisitRelation;
    }
}
